package com.tony.pmp.server.controller;

import com.google.common.collect.Maps;
import com.tony.pmp.common.response.BaseResponse;
import com.tony.pmp.common.response.StatusCode;
import com.tony.pmp.common.utils.PageUtil;
import com.tony.pmp.model.entity.AttendRecordEntity;
import com.tony.pmp.server.common.PoiService;
import com.tony.pmp.server.common.WebOperationService;
import com.tony.pmp.server.service.AttendRecordService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: AttendRecordController
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/03/15:08
 * @Description: 考勤
 */
@Controller
@RequestMapping("/attend/record")
public class AttendRecordController extends AbstractController{

    @Autowired
    private AttendRecordService attendRecordService;
    @Autowired
    private PoiService poiService;
    @Autowired
    private WebOperationService webOperationService;

    /**
    * @Description: list 获取考勤列表
    * @Param: [params]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/3 15:16
    */
    @ResponseBody
    @RequestMapping("/list")
    public BaseResponse list(@RequestParam Map<String,Object> params) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            log.info(">>>>>>获取考勤列表数据>>>>>>");
            PageUtil page = attendRecordService.queryPage(params);
            resMap.put("page",page);

        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: export 导出excel
    * @Param: [params, response]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    * @Date: 2021/8/4 11:36
    */
    @ResponseBody
    @RequestMapping("/export")
    public String export(@RequestParam Map<String,Object> params, HttpServletResponse response) {
        final String[] headers = new String[]{"ID","部门名称","姓名","日期","打卡状态","打卡开始时间","打卡结束时间","工时/小时"};
        try {
            String fileName = new StringBuilder("考勤明细-").append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())).toString();
            String excelName = fileName + ".xls";

            //针对具体的业务模块，查询相应的数据，并做“行转列映射”处理
                //获取数据
            List<AttendRecordEntity> list = attendRecordService.selectAll(params);
                //中间站转换处理为导出服务
            List<Map<Integer,Object>> listMap = attendRecordService.manageExport(list);

            //通用
                // 添加数据到excel的sheet中
            Workbook workbook = poiService.fillExcelSheetData(listMap,headers,fileName);
                //下载Excel
            webOperationService.downloadExcel(response,workbook,excelName);
            return excelName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }







}
